<template>
    <div class="zijin_liushui">
        <div class="title">
            <span>转账</span>
            <i class="iconfonMy icon-guanbi1" @click="ipcRenderer.send('send-shuaxin-close')"></i>
        </div>
        <div class="zijin_body">
            <div class="b_left">
                <div class="zhanghao">
                    <span>交易账号</span>
                    <span class="z_h">{{ dataInfo.bh }}</span>
                </div>
                <div class="b_left_body_title">
                    <span :class="{ active: ChuRuType == 1 }" @click="ChuRuType = 1">银行转期货</span>
                    <span :class="{ active: ChuRuType == 2 }" @click=" ChuRuType = 2">期货转银行</span>
                </div>
                <div class="b_left_body">
                    <select class="yin_hang" v-model="jiekou_id">
                        <option :value="item.id" v-for="item in tongdaoInfo" :key="item.id">{{ item.name }}</option>
                    </select>
                    <div class="itm_li">
                        <span>资金密码</span>
                        <input v-model="fund_pass" type="password">
                    </div>
                    <!-- <div class="itm_li">
                        <span>银行密码</span>
                        <input type="password">
                    </div> -->
                    <div class="itm_li" v-if="ChuRuType == 1">
                        <span>转账金额</span>
                        <div class="itm_li_right">
                            <input type="number" v-model="moneyrujin" min="1" step="1" @input="heardInout" />>
                            <select v-model="hb_id_rujin">
                                <option :value="item.id" v-for="item in huilvRuJin" :key="item.id">{{
                                    item.name == '美元' ? 'USD' : item.name == "人民币" ? 'CNY' : item.name }}
                                </option>
                            </select>
                        </div>
                    </div>
                    <div class="itm_li" v-if="ChuRuType == 2">
                        <span>转账金额</span>
                        <div class="itm_li_right">
                            <input type="number" v-model="moneychujin" min="1" step="1" @input="heardInout" />>
                            <select v-model="hb_id_chujin">
                                <option :value="item.id" v-for="item in huilvChuJin" :key="item.id">{{
                                    item.name == '美元' ? 'USD' : item.name == "人民币" ? 'CNY' : item.name }}
                                </option>
                            </select>
                        </div>
                    </div>
                    <!-- <div class="itm_li" v-if="ChuRuType == 1">
                        <span>转入金额：{{ zHMoney.rujin }} 美元</span>
                    </div>
                    <div class="itm_li" v-if="ChuRuType == 2">
                        <span>转出金额：{{ zHMoney.chujin }} 人民币</span>
                    </div> -->
                    <div class="btns">
                        <span @click="checkTheBalance" v-if="ChuRuType == 1">查余额</span>
                        <span @click="openZhuanzhang">转账</span>
                        <i class="message" v-if="ChuRuType == 1 && zhanghuYue != null">账户余额：{{ zhanghuYue }}</i>
                        <i class="message" v-if="ChuRuType == 2">期货可转：{{ setChujin(dataInfo.zijin) }}</i>
                    </div>
                </div>
            </div>
            <div class="b_right">
                <div class="b_right_title">
                    <span>资金流水</span>
                    <span class="shuqa_xin" @click="refreshTap">刷新流水</span>
                </div>
                <el-table :data="liushuiList" style="width: 445px;border: 1px solid #828790;" height="272" size="small"
                    border>
                    <el-table-column show-overflow-tooltip prop="单号" label="流水号" width="60"></el-table-column>
                    <el-table-column show-overflow-tooltip label="操作时间">
                        <template #default="scope">
                            <div>{{ moment(scope.row['时间'] * 1000).format('HH:mm:ss') }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column show-overflow-tooltip prop="流水类别" label="流水类别"></el-table-column>
                    <el-table-column show-overflow-tooltip prop="流水金额" label="金额"></el-table-column>
                    <el-table-column show-overflow-tooltip prop="余额" label="余额"></el-table-column>
                    <el-table-column show-overflow-tooltip prop="货币名称" label="币种名称" width="61"></el-table-column>
                </el-table>
            </div>
        </div>
        <div class="rujinDiog" v-if="showRuJinMessage">
            <div class="body">
                <div class="nav_bar">
                    <div>请付款 （${{ RuJinMessageInfo.meiyuan }}）</div>
                    <div v-if="tongdaoInfo.find(item => item.id == jiekou_id)?.name == 'USDT'">USDT{{ RuJinMessageInfo.benbi
                    }}</div>
                    <div v-else>￥{{ RuJinMessageInfo.benbi }}</div>
                    <img :src="tongdaoInfo.find(item => item.id == jiekou_id)?.logo" alt="">
                </div>
                <div class="type">{{ tongdaoInfo.filter(item => item.id == jiekou_id)[0]?.name }}</div>
                <el-image class="erwei_ma" v-if="RuJinMessageInfo.type != 1" :src="RuJinMessageInfo.branch" :zoom-rate="1.2"
                    :max-scale="7" :min-scale="0.2" :preview-src-list="[RuJinMessageInfo.branch]" fit="cover"
                    @contextmenu.prevent="saveImg(RuJinMessageInfo.branch)" />

                <div class="order_info" style="justify-content: center;padding: 0;" v-if="RuJinMessageInfo.type == 1">
                    <img :src="tongdaoInfo.find(item => item.id == jiekou_id)?.logo" alt="">
                </div>
                <div class="order_info" v-if="RuJinMessageInfo.type == 1">
                    <div>
                        <span class="name"> 收款人姓名：</span>
                        <span>{{ RuJinMessageInfo.name }}</span>
                    </div>
                    <span @click="copyVal(RuJinMessageInfo.name)">复制</span>
                </div>
                <div class="order_info">
                    <div>
                        <span class="name" v-if="RuJinMessageInfo.type == 1"> 收款人卡号：</span>
                        <span class="name" v-if="RuJinMessageInfo.type == 2"> 支付宝账号：</span>
                        <span class="name" v-if="RuJinMessageInfo.type == 3"> 微信账号：</span>
                        <span class="name" v-if="RuJinMessageInfo.type == 4"> USDT地址：</span>
                        <span>{{ RuJinMessageInfo.card_no }}</span>
                    </div>
                    <span @click="copyVal(RuJinMessageInfo.card_no)">复制</span>
                </div>
                <div class="order_info" v-if="RuJinMessageInfo.type == 1">
                    <div>
                        <span class="name"> 收款银行：</span>
                        <span>{{ RuJinMessageInfo.branch }}</span>
                    </div>
                    <span @click="copyVal(RuJinMessageInfo.branch)">复制</span>
                </div>
                <div class="order_info" v-if="RuJinMessageInfo.type == 1">
                    <div>
                        <span class="name"> 安全码：</span>
                        <span>{{ RuJinMessageInfo.fuyan }}</span>
                    </div>
                    <span @click="copyVal(RuJinMessageInfo.fuyan)">复制</span>
                </div>
                <div class="user_info">
                    <span>用户姓名</span>
                    <span>{{ dataInfo?.loginInfo?.xingming }}</span>
                </div>
                <div class="user_info">
                    <span>用户账户</span>
                    <span>{{ dataInfo.bh }}</span>
                </div>
                <div class="user_info">
                    <span>订单号</span>
                    <span>{{ RuJinMessageInfo.bh }}</span>
                </div>
                <div class="btns">
                    <span @click="sumbitFuKuan(2)">取消订单</span>
                    <span @click="sumbitFuKuan(1)">我已付款成功</span>
                </div>
            </div>
        </div>
        <div class="rujinDiog" v-if="showChuJinMessage">
            <div class="body">
                <div class="nav_bar">
                    <div>出金账户信息</div>
                </div>
                <div class="order_info">
                    <div>
                        <span class="name"> 银行卡号：</span>
                        <span>{{ dataInfo.loginInfo?.bank_bh }}</span>
                    </div>
                    <span @click="copyVal(dataInfo.loginInfo?.bank_bh)">复制</span>
                </div>
                <div class="order_info">
                    <div>
                        <span class="name"> 户名：</span>
                        <span>{{ dataInfo.loginInfo?.xingming }}</span>
                    </div>
                    <span @click="copyVal(dataInfo.loginInfo?.xingming)">复制</span>
                </div>
                <div class="order_info">
                    <div>
                        <span class="name"> 银行：</span>
                        <span>{{ dataInfo.loginInfo?.bank }}</span>
                    </div>
                    <span @click="copyVal(dataInfo.loginInfo?.bank)">复制</span>
                </div>
                <div class="btns chujin_btns">
                    <span @click="showChuJinMessage = false">确定</span>
                </div>
            </div>
        </div>

    </div>
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
const { ipcRenderer } = require('electron');
import { ElMessage, ElMessageBox } from 'element-plus';
import moment from 'moment'
import { throttle } from 'lodash';
import axios from 'axios';
import { mainStore } from '@/store/index.js'
const CryptoJS = require('crypto-js');


const storeMain = mainStore()


const ChuRuType = ref(1)
const jiekou_id = ref('')
const hb_id_rujin = ref()
const hb_id_chujin = ref()
const dataInfo = ref({})
const huilvRuJin = ref([])
const huilvChuJin = ref([])
const liushuiList = ref([])

const moneyrujin = ref(0)
const moneychujin = ref(0)
const zHMoney = reactive({
    rujin: 0,
    chujin: 0
})

const tongdaoInfo = ref([])
const showRuJinMessage = ref(false)
const RuJinMessageInfo = ref({})
const fund_pass = ref('')

const showChuJinMessage = ref(false)
const chuJinMessageInfo = ref({})
const zhanghuYue = ref(null)

watch(jiekou_id, newVal => {
    let name = tongdaoInfo.value.find(item => item.id == newVal)?.name
    if (name == 'USDT') {
        huilvRuJin.value = dataInfo.value.huilvdetailInfo.filter(item => item.name == 'USDT')
        huilvChuJin.value = dataInfo.value.huilvdetailInfo.filter(item => item.name == 'USDT')
    } else {
        console.log(`output->3369`, dataInfo.value.huilvdetailInfo)
        huilvRuJin.value = dataInfo.value.huilvdetailInfo.filter(item => item.name == '人民币')
        huilvChuJin.value = dataInfo.value.huilvdetailInfo.filter(item => item.name == '人民币')
    }
    hb_id_rujin.value = huilvRuJin.value[0].id
    hb_id_chujin.value = huilvChuJin.value[0].id
    heardInout()
})

ipcRenderer.on('send-zhanghu-yue', (e, v) => {
    zhanghuYue.value = v
    ElMessage({ message: '查询成功', type: 'success' });
})

ipcRenderer.on('send-zijinLiushui', (e, v) => {
    dataInfo.value = JSON.parse(v)
    showChuJinMessage.value = false
    liushuiList.value = dataInfo.value.dataArr.map(item => {
        return {
            '流水ID': item[2],
            '时间': item[3],
            '流水类别': item[4],
            '单号': item[5],
            '流水金额': item[6],
            '余额': item[7],
            '货币名称': item[8]
        }
    })
    if (!dataInfo.value.ischurujinRefresh) {
        getgettongdao()
        huilvRuJin.value = dataInfo.value.huilvdetailInfo.filter(item => item.name == '人民币')
        huilvChuJin.value = dataInfo.value.huilvdetailInfo.filter(item => item.name == '人民币')
        hb_id_rujin.value = huilvRuJin.value[0].id
        hb_id_chujin.value = huilvChuJin.value[0].id
    }
})
ipcRenderer.on('save-image-reply', (e, v) => {
    console.log(`output->v`, v)
    if (v.success) {
        return ElMessage({
            type: 'success',
            message: '保存成功',
        })
    }
    return ElMessage({
        type: 'error',
        message: '保存失败',
    })
})
function saveImg(url) {
    ElMessageBox.confirm(
        '保存二维码?',
        'info',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'info',
        }
    )
        .then(() => {
            ipcRenderer.send('save-image', url)
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消保存',
            })
        })
}
function copyVal(v) {
    navigator.clipboard.writeText(String(v)).then(() => {
        ElMessage({ message: '已成功复制到剪切板', type: 'success' });
    }).catch(() => {
        ElMessage({ message: '复制失败', type: 'error' });
    });
}
async function sumbitFuKuan(flag) {
    let { data } = await axios.post(storeMain.xianlu.url + '/rujingl', { flag, bh: RuJinMessageInfo.value.bh }, {
        headers: {                // 自定义的请求头
            'token': dataInfo.value.token,
        }
    })
    if (data.code == 0) {
        if (flag == 1) {
            let msg = tongdaoInfo.value.find(item => item.id == jiekou_id.value)?.name
            ElMessage({ message: `你已通过${msg} 入金：${RuJinMessageInfo.value.benbi}元！`, type: 'success' });
        }
        if (flag == 2) {
            ElMessage({ message: `你已取消订单`, type: 'success' });
        }
        showRuJinMessage.value = false
    } else {
        ElMessage({ message: data.msg, type: 'error' });
    }

}
function setChujin(v) {
    let price = 0
    if (v['浮动盈亏'] * 1 < 0) {
        price = v['账户余额'] * 1 + v['浮动盈亏'] * 1 - v['占用保证金'] * 1
        price = parseFloat(Number(price).toFixed(2))
    }
    if (v['浮动盈亏'] * 1 >= 0) {
        price = v['账户余额'] * 1 - v['占用保证金'] * 1
        price = parseFloat(Number(price).toFixed(2))
    }
    return price
}
async function getgettongdao() {
    let { data } = await axios.post(storeMain.xianlu.url + '/gettongdao', { type: 1 }, {
        headers: {                // 自定义的请求头
            'token': dataInfo.value.token,
        }
    })
    if (data.code == 0) {
        tongdaoInfo.value = data.data
        jiekou_id.value = tongdaoInfo.value[0].id
    } else {
        ElMessage({ message: data.msg, type: 'error' });
    }
}

const refreshTap = () => {
    liushuiList.value = []
    ipcRenderer.send('send-shuaxin-close', 1)
}
const checkTheBalance = throttle(async function () {
    ipcRenderer.send('placeAnOrderWindow_click_msg', { type: '查余额' })
}, 1000)
const openZhuanzhang = throttle(async function () {
    let url = '',
        hb_id, money
    if (ChuRuType.value == 1) {
        url = '/rujin'
        hb_id = hb_id_rujin.value
        money = moneyrujin.value
    }
    if (ChuRuType.value == 2) {
        if (!fund_pass.value) {
            return ElMessage({ message: '请填写资金密码', type: 'error' })
        }
        url = '/chujin'
        hb_id = hb_id_chujin.value
        money = moneychujin.value
    }
    if (!money) {
        return ElMessage({ message: '转账金额填写错误！', type: 'error' })
    }
    let { data } = await axios.post(storeMain.xianlu.url + url, {
        zhongduan: 1,
        money,
        hb_id,
        jiekou_id: jiekou_id.value,
        fund_pass: md5Encrypt(fund_pass.value),
    }, {
        headers: {                // 自定义的请求头
            'token': dataInfo.value.token,
        }
    })
    if (data.code == 0) {
        // ElMessage({ message: data.msg, type: 'success' });
        if (ChuRuType.value == 1) {
            showRuJinMessage.value = true
            RuJinMessageInfo.value = data.data
        }
        if (ChuRuType.value == 2) {
            showChuJinMessage.value = true
            chuJinMessageInfo.value = data.data
            ElMessage({ message: data.msg, type: 'success' });
        }
        liushuiList.value = []
        ipcRenderer.send('send-shuaxin-close', 1)
    } else {
        ElMessage({ message: data.msg, type: 'error' });
    }
}, 1000)

function heardInout() {
    if (ChuRuType.value == 1) {
        moneyrujin.value = moneyrujin.value <= 0 ? 0 : moneyrujin.value
        let obj = dataInfo.value.huilvdetailInfo.find(item => item.name == '人民币')
        let price = parseFloat((moneyrujin.value * obj.bilv / huilvRuJin.value[0].bilv).toFixed(2))
        zHMoney.rujin = price && !isNaN(price) ? price : 0
    }
    if (ChuRuType.value == 2) {
        moneychujin.value = moneychujin.value <= 0 ? 0 : moneychujin.value
        let obj = dataInfo.value.huilvdetailInfo.find(item => item.name == '人民币')
        let price = parseFloat((moneychujin.value * obj.bilv / huilvChuJin.value[0].bilv).toFixed(2))
        zHMoney.chujin = price && !isNaN(price) ? price : 0
    }
}

function md5Encrypt(text) {
    return CryptoJS.MD5(text).toString();
}

</script>

<style scoped lang="less">
.zijin_liushui {
    background-color: #f0f0f0;
    color: #000;
    width: 720px;
    height: 340px;
    overflow: hidden;
    box-sizing: border-box;

    .title {
        background-color: #f3f3f3;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-left: 5px;
        line-height: 25px;
        -webkit-app-region: drag;


        .icon-guanbi1 {
            width: 25px;
            height: 25px;
            text-align: center;
            -webkit-app-region: no-drag;

        }
    }

    .zijin_body {
        display: flex;
        padding: 10px;

        .zhanghao {
            display: flex;
            align-items: center;
            line-height: 20px;

            .z_h {
                border-radius: 3px;
                margin-left: 10px;
                border: 1px solid #d2d2d2;
                padding: 0 5px;
                width: 115px;
                background-color: #fff;
            }
        }

        .b_left_body_title {
            display: flex;
            align-items: center;
            line-height: 20px;
            margin-top: 15px;
            color: #666;

            span {
                background-color: #dbdbdb;
                width: 75px;
                text-align: center;
            }

            .active {
                color: #000;
                background-color: #ffffff;
            }
        }

        .b_left_body {
            border: 1px solid #dcdcdc;
            margin-right: 10px;
            width: 245px;
            height: 235px;
            padding: 15px 10px;

            .yin_hang {
                width: 100%;
                font-size: 12px;
                font-family: '宋体';
                color: #000;
            }

            .itm_li {
                display: flex;
                align-items: center;
                padding-top: 15px;

                span {
                    flex: 1;
                }

                input {
                    width: 167px;
                }

                .itm_li_right {
                    width: 167px;
                    display: flex;
                    justify-content: space-between;

                    input {
                        width: 95px;
                    }

                    select {
                        width: 62px;
                        font-size: 12px;
                        font-family: '宋体';
                        color: #000;
                    }
                }
            }

            .btns {
                display: flex;
                align-items: center;
                padding-top: 15px;
                text-align: right;
                width: 177px;
                margin-left: calc(100% - 177px);
                position: relative;

                span {
                    flex: 1;
                    display: inline-block;
                    border: 1px solid #d0d0d0;
                    border-radius: 3px;
                    margin-left: 10px;
                    line-height: 22px;
                    background-color: #fff;
                    text-align: center;

                }

                span:hover {
                    border-color: #0078d4;
                }

                .message {
                    color: #595974;
                    font-style: normal;
                    position: absolute;
                    bottom: -20px;
                    left: 10px;
                }
            }
        }

        .b_right {
            .b_right_title {
                span {
                    display: inline-block;
                    background-color: #fff;
                    padding: 2px 8px;
                }

                .shuqa_xin {
                    float: right;
                    background: transparent;
                }
            }
        }
    }

    .rujinDiog {
        background-color: rgba(0, 0, 0, 0.7);
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 100;
        animation: fadeIn 0.3s ease-in-out;

        .body {
            background-color: #f0f0f0;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            border-radius: 5px;
            width: 400px;

            .nav_bar {
                background-color: #d5d8e6;
                border-radius: 5px 5px 0 0;
                padding: 5px;
                line-height: 20px;
                position: relative;

                img {
                    position: absolute;
                    right: 10px;
                    width: auto;
                    height: 40px;
                    z-index: 200;
                    top: 50%;
                    transform: translateY(-50%);
                }
            }

            .type {
                background-color: #f0f0f0;
                line-height: 20px;
                padding: 0 5px;
            }

            .erwei_ma {
                display: block;
                margin: 0 auto;
                width: 70px;
            }

            .order_info {
                display: flex;
                align-items: center;
                justify-content: space-between;
                padding: 5px;

                div {
                    display: flex;
                    align-items: center;

                    .name {
                        width: 80px;
                        text-align: right;
                    }
                }

                >span {
                    background-color: #fdfdfd;
                    border: 1px solid #d0d0d0;
                    width: 60px;
                    text-align: center;
                    border-radius: 3px;
                }

                >span:hover {
                    background-color: #e0eef9;
                    border: 1px solid #0078d4;
                }
            }

            .user_info {
                padding: 2px 5px;
                display: flex;
                align-items: center;
                justify-content: space-between;
                background-color: #fff;
            }

            .btns {
                text-align: right;
                padding: 10px 5px;

                span {
                    padding: 2px 10px;
                    border: 1px solid #d0d0d0;
                    background-color: #fdfdfd;
                    border-radius: 3px;
                    margin-left: 10px;
                }

                span:nth-of-type(2) {
                    background-color: #0000ff;
                    color: #fff;
                }
            }

            .chujin_btns {
                span {
                    display: inline-block;
                    background-color: #0000ff;
                    color: #fff;
                    width: 70px;
                    text-align: center;
                }
            }
        }
    }
}

@keyframes fadeIn {
    from {
        opacity: 0.9;
    }

    to {
        opacity: 1;
    }
}

:deep(.el-table th.el-table__cell>.cell),
:deep(.el-table td.el-table__cell div) {
    padding: 0;
    padding-left: 3px;
    text-align: left;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

:deep(.el-table th.el-table__cell.is-leaf),
:deep(.el-table--enable-row-transition .el-table__body td.el-table__cell) {
    padding: 0;
    color: #000000;
    font-weight: normal;
}

:deep(.el-table th.el-table__cell.is-leaf) {
    background-color: #ffffff;
    border-color: #f0f0f0;
}

:deep(.el-table--enable-row-transition .el-table__body td.el-table__cell) {
    background-color: #f1f8ff;
    border-color: #f0f0f0;
}

:deep(.el-table--enable-row-transition .el-table__body td.el-table__cell .cell) {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

:deep(.el-scrollbar__bar.is-horizontal) {
    height: 0;
}

:deep(.el-table--scrollable-y .el-table__body-wrapper::-webkit-scrollbar) {
    width: 0px;
    height: 7px;
}

:deep(.el-table--scrollable-y .el-table__body-wrapper::-webkit-scrollbar-thumb) {
    background: #c0c0c0;
    border-radius: 5px;
}

:deep(.el-table--scrollable-y .el-table__body-wrapper::-webkit-scrollbar-thumb:hover) {
    background: #c0c0c0;
}

:deep(.el-table--border .el-table__cell) {
    background-color: #f1f8ff;
    padding: 0;
    color: #000;
}
</style>
